package com.wallpaper.weatherwallpaper;

import com.model.weather.DayTime;
import com.model.weather.Weather;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class ViewWallPaperActivity extends Activity {

	Button saveWallPaperButton;
	ImageView pictureImageView;
	String[] weatherStr={
			Weather.Sunny.getWeatherName(),
			Weather.Rainy.getWeatherName(),
			Weather.Cloudy.getWeatherName(),
			Weather.Snowy.getWeatherName(),
			Weather.Calm.getWeatherName(),
	};
	String[] timeStr={
			DayTime.Day.getDayTime(),
			DayTime.Night.getDayTime(),
	};
	Spinner weatherSpinner;
	Spinner timeSpinner;
	BaseAdapter weatherBA;
	BaseAdapter timeBA;
	String weatherSelected;
	String timeSelected;
	int pathInt;
	String pathStr;
	
	
	//定义SharedPreferences文件中的键
	private final String SunnyDayInt = "SunnyDayInt";
	private final String SunnyDayStr="SunnyDayStr";
	private final String SunnyNightInt="SunnyNightInt";
	private final String SunnyNightStr="SunnyNightStr";
	private final String RainyDayInt = "RainyDayInt";
	private final String RainyDayStr="RainyDayStr";
	private final String RainyNightInt="RainyNightInt";
	private final String RainyNightStr="RainyNightStr";
	private final String CloudyDayInt = "CloudyDayInt";
	private final String CloudyDayStr="CloudyDayStr";
	private final String CloudyNightInt="CloudyNightInt";
	private final String CloudyNightStr="CloudyNightStr";
	private final String SnowyDayInt = "SnowyDayInt";
	private final String SnowyDayStr="SnowyDayStr";
	private final String SnowyNightInt="SnowyNightInt";
	private final String SnowyNightStr="SnowyNightStr";
	private final String CalmDayInt = "CalmDayInt";
	private final String CalmDayStr="CalmDayStr";
	private final String CalmNightInt="CalmNightInt";
	private final String CalmNightStr="CalmNightStr";
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_view_wall_paper);
		
		Bundle bundle=this.getIntent().getExtras();
		weatherSelected=bundle.getString("Weather");
		timeSelected=bundle.getString("Time");
		pathInt=bundle.getInt("pathInt");
		pathStr=bundle.getString("pathStr");
		
		initImageView();
		initWeatherSpinner();
		initTimeSpinner();
		initSaveWallPaperButton();

	}

	private void initSaveWallPaperButton() {
		// TODO Auto-generated method stub
		saveWallPaperButton=(Button)findViewById(R.id.SaveWallPaperButton);
		saveWallPaperButton.setOnClickListener(new OnClickListener(){

			public void onClick(View v) {
				// TODO Auto-generated method stub
				SharedPreferences userPreferences = getSharedPreferences("WeatherWP",Context.MODE_PRIVATE);
				SharedPreferences.Editor editor = userPreferences.edit();
				if(weatherSelected.equals("晴天")){
					if(timeSelected.equals("白天")){
						if(pathInt!=0){
							editor.putInt(SunnyDayInt, pathInt);
							editor.putString(SunnyDayStr, null);
						}else{
							editor.putInt(SunnyDayInt, 0);
							editor.putString(SunnyDayStr, pathStr);
						}
					}else{
						if(pathInt!=0){
							editor.putInt(SunnyNightInt, pathInt);
							editor.putString(SunnyNightStr, null);
						}else{
							editor.putInt(SunnyNightInt, 0);
							editor.putString(SunnyNightStr, pathStr);
						}
					}				
				}else if(weatherSelected.equals("雨")){
					if(timeSelected.equals("白天")){
						if(pathInt!=0){
							editor.putInt(RainyDayInt, pathInt);
							editor.putString(RainyDayStr, null);
						}else{
							editor.putInt(RainyDayInt, 0);
							editor.putString(RainyDayStr, pathStr);
						}
					}else{
						if(pathInt!=0){
							editor.putInt(RainyNightInt, pathInt);
							editor.putString(RainyNightStr, null);
						}else{
							editor.putInt(RainyNightInt, 0);
							editor.putString(RainyNightStr, pathStr);
						}
					}
				}else if(weatherSelected.equals("阴天")){
					if(timeSelected.equals("白天")){
						if(pathInt!=0){
							editor.putInt(CloudyDayInt, pathInt);
							editor.putString(CloudyDayStr, null);
						}else{
							editor.putInt(CloudyDayInt, 0);
							editor.putString(CloudyDayStr, pathStr);
						}
					}else{
						if(pathInt!=0){
							editor.putInt(CloudyNightInt, pathInt);
							editor.putString(CloudyNightStr, null);
						}else{
							editor.putInt(CloudyNightInt, 0);
							editor.putString(CloudyNightStr, pathStr);
						}
					}
				}else if(weatherSelected.equals("雪")){
					if(timeSelected.equals("白天")){
						if(pathInt!=0){
							editor.putInt(SnowyDayInt, pathInt);
							editor.putString(SnowyDayStr, null);
						}else{
							editor.putInt(SnowyDayInt, 0);
							editor.putString(SnowyDayStr, pathStr);
						}
					}else{
						if(pathInt!=0){
							editor.putInt(SnowyNightInt, pathInt);
							editor.putString(SnowyNightStr, null);
						}else{
							editor.putInt(SnowyNightInt, 0);
							editor.putString(SnowyNightStr, pathStr);
						}
					}
				}else if(weatherSelected.equals("风平浪静")){
					if(timeSelected.equals("白天")){
						if(pathInt!=0){
							editor.putInt(CalmDayInt, pathInt);
							editor.putString(CalmDayStr, null);
						}else{
							editor.putInt(CalmDayInt, 0);
							editor.putString(CalmDayStr, pathStr);
						}
					}else{
						if(pathInt!=0){
							editor.putInt(CalmNightInt, pathInt);
							editor.putString(CalmNightStr, null);
						}else{
							editor.putInt(CalmNightInt, 0);
							editor.putString(CalmNightStr, pathStr);
						}
					}
				}
				editor.commit();
				
				Toast.makeText(ViewWallPaperActivity.this, "已保存设置！", Toast.LENGTH_SHORT).show();
				
				Intent intent=new Intent();
				intent.setClass(ViewWallPaperActivity.this,SetWallPaperActivity.class);
				startActivity(intent);
			}
			
		});
	}

	private void initTimeSpinner() {
		// TODO Auto-generated method stub
		timeSpinner=(Spinner)findViewById(R.id.TimeSpinner);
		timeBA=new BaseAdapter(){

			public int getCount() {
				// TODO Auto-generated method stub
				return 2;
			}

			public Object getItem(int position) {
				// TODO Auto-generated method stub
				return null;
			}

			public long getItemId(int position) {
				// TODO Auto-generated method stub
				return 0;
			}

			public View getView(int position, View convertView, ViewGroup parent) {
				TextView tv=new TextView(ViewWallPaperActivity.this);
				tv.setText(timeStr[position]);
				tv.setTextSize(16);
				return tv;
			}
			
		};
		timeSpinner.setAdapter(timeBA);
		timeSpinner.setOnItemSelectedListener(new OnItemSelectedListener(){

			public void onItemSelected(AdapterView<?> arg0, View v,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				TextView tvSelected=(TextView)v;
				timeSelected=tvSelected.getText().toString();
			}

			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
			
		});
		if(timeSelected!=null){
			int timeSelectedIndex=findTimeIndex(timeSelected);
			timeSpinner.setSelection(timeSelectedIndex);
		}
		
	}

	private int findTimeIndex(String timeSelected2) {
		// TODO Auto-generated method stub
		for(int i=0;i<timeStr.length;i++){
			if(timeStr[i].equals(timeSelected2)){
				return i;
			}
		}
		return 0;
	}

	private void initWeatherSpinner() {
		// TODO Auto-generated method stub
		weatherSpinner=(Spinner)findViewById(R.id.WeatherSpinner);
		weatherBA=new BaseAdapter(){

			public int getCount() {
				// TODO Auto-generated method stub
				return 5;
			}

			public Object getItem(int arg0) {
				// TODO Auto-generated method stub
				return null;
			}

			public long getItemId(int position) {
				// TODO Auto-generated method stub
				return 0;
			}

			public View getView(int position, View convertView, ViewGroup parent) {
				// TODO Auto-generated method stub
				TextView tv=new TextView(ViewWallPaperActivity.this);
				tv.setText(weatherStr[position]);
				tv.setTextSize(16);
				return tv;
			}
			
		};
		weatherSpinner.setAdapter(weatherBA);
		weatherSpinner.setOnItemSelectedListener(new OnItemSelectedListener(){

			public void onItemSelected(AdapterView<?> arg0, View v,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				TextView tvSelected=(TextView)v;
				weatherSelected=tvSelected.getText().toString();
			}

			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
			
		});
		if(weatherSelected!=null){
			int weatherSelectedIndex=findWeatherIndex(weatherSelected);
			weatherSpinner.setSelection(weatherSelectedIndex);
		}
		
	}

	private int findWeatherIndex(String weatherSelected2) {
		// TODO Auto-generated method stub
		for(int i=0;i<weatherStr.length;i++){
			if(weatherStr[i].equals(weatherSelected2)){
				return i;
			}
		}
		return 0;
	}

	private void initImageView() {
		// TODO Auto-generated method stub
		pictureImageView=(ImageView)findViewById(R.id.PictureImageView);
		Bitmap bm;
		if(pathInt!=0){
			bm=BitmapFactory.decodeResource(this.getResources(),pathInt);		
		}else{
			bm=BitmapFactory.decodeFile(pathStr);
		}
		pictureImageView.setImageBitmap(bm);
	}
}
